import { Controller, Post, Body, Req, UseGuards } from '@nestjs/common'
import { RefundService } from './refund.service'
import { DelRefundDto, ListRefundDto, RefundApplyDto, RefundOperateDto } from './dto/refund.dto'
import { Request } from 'express'
import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger'
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
import { RateLimit } from '../common/rateLimit/rate-limit.decorator'

@ApiTags('order')
@Controller('order')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT')
export class RefundController {
  constructor(private readonly refundService: RefundService) { }

  // 用户申请退款
  @ApiOperation({ summary: '订单退款申请' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(5, 60 * 1000)
  @Post('refund/apply')
  async applyRefund(@Body() data: RefundApplyDto, @Req() req: Request) {
    const userId = req.user?.id as number
    await this.refundService.applyRefund(data, userId)
    return {
      isAdd: true
    }
  }

  // 处理退款
  @ApiOperation({ summary: '订单退款处理' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(5, 60 * 1000)
  @Post('refund/operate')
  async handleRefund(@Body() data: RefundOperateDto, @Req() req: Request) {
    const userId = req.user?.id as number
    await this.refundService.handleRefund(data, userId)
    return {
      isAdd: true
    }
  }

  // 退款list
  @ApiOperation({ summary: '订单退款list' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('refund/list')
  async getRefundList(@Body() data: ListRefundDto) {
    const list = await this.refundService.refundList(data)
    return list
  }

  // 退款订单删除
  @ApiOperation({ summary: '订单退款删除' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(5, 60 * 1000)
  @Post('refund/delete')
  async deleteRefund(@Body() data: DelRefundDto) {
    await this.refundService.deleteRefund(data)
    return {
      isAdd: true
    }
  }
}
